package com.bw.mallshop.mvvm.viewmodel;

import android.view.View;
import androidx.annotation.NonNull;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.adapter.CategoryAdapter;
import com.bw.mallshop.adapter.CategoryValueAdapter;
import com.bw.mallshop.entity.CategoryEntity;
import com.bw.mallshop.entity.CategoryValueEntity;
import com.bw.mallshop.mvvm.model.CategoryModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CategoryViewModel extends BaseViewModel<CategoryModel> implements
        OnItemClickListener {
    public List<CategoryEntity>leftList = new ArrayList<>();
    public CategoryAdapter categoryAdapter;
    public CategoryValueAdapter categoryValueAdapter;
    public int categoryType = 0;
    public int valueType = 1;

    @Override
    protected void initData() {
        categoryAdapter = new CategoryAdapter();
        categoryValueAdapter = new CategoryValueAdapter();
        leftList.addAll(m.requestCategory());
        categoryAdapter.setOnItemClickListener(this);
        categoryValueAdapter.setOnItemClickListener(this);
        categoryAdapter.setNewInstance(leftList);
        //获取默认第0条索引对应数据
        requestCategoryValue(leftList.get(0).parent_id);
    }

    //封装请求右侧categoryvalue 列表数据
    private void requestCategoryValue(int parentId){
        Map<String,Object>map = new HashMap<>();
        map.put("parentId",parentId);
        m.requestCategoryValue(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof CategoryValueEntity){
            CategoryValueEntity categoryValueEntity = (CategoryValueEntity) entity;
            if (categoryValueEntity == null)
                return;
            if (categoryValueEntity.data == null)
                return;
            categoryValueAdapter.setNewInstance(categoryValueEntity.data);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
          if (adapter instanceof CategoryAdapter){
              //左侧列表被点击:2种写法:先获取到原有选中得item，selflag 更改为false，点击得这条item数据selflag设置为true
                                     //全部设置为false，在把点击得设置为true
              CategoryAdapter categoryAdapter = (CategoryAdapter) adapter;
              int index = 0;
              //获取原有得选中索引
              for (CategoryEntity entity : categoryAdapter.getData()){
                  if (entity.selFlag){
                      index = entity.parent_id;//由于parentid按照创建索引复制->直接使用
                      break;
                  }
              }
              //区分原有选中item和用户点击得item是否为同一条数据
              //同一条数据直接跳出不需要修改以及请求最新数据
              if (index == position)
                  return;
              //原有item设置为false，新点击得item设置为true->获取右侧列表数据
              CategoryEntity entity = categoryAdapter.getItem(index);
              entity.selFlag = false;
              categoryAdapter.setData(index,entity);
              CategoryEntity itemEntity = categoryAdapter.getItem(position);
              itemEntity.selFlag = true;
              categoryAdapter.setData(position,itemEntity);
              //请求右侧列表数据
              requestCategoryValue(itemEntity.parent_id);
          }else {
              //右侧列表被点击->跳转商品详情
//              startPage();
          }
    }
}
